package org.ceteca.explica.client;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.Iterator;

import org.ceteca.javaglade.toolkit.swt.StatusBar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class BuildGuiOperation implements Runnable {
	private Display display;
	private ProgressBar progressBar;
	private Shell initWindow;
	private StatusBar statusBar;
	private Object builderInstance;
	private String builderMethodName;
	private Method builderMethod;
	
	public BuildGuiOperation(Display display, ProgressBar progressBar, Shell initWindow, StatusBar statusBar, Object builderInstance, String builderMethodName) {
		this.display = display;
		this.progressBar = progressBar;
		this.initWindow = initWindow;
		this.statusBar = statusBar;
		this.builderInstance = builderInstance;
		this.builderMethodName = builderMethodName;
		if (builderInstance != null && builderMethodName != null) {
			try {
				this.builderMethod = this.builderInstance.getClass().getMethod(builderMethodName, (Class[])null);
			}
			catch (Exception ex) {
				ClientContext.getInstance().getLogger().error("No GUI builder method has been found!", ex);
			}
		}
	}
	
	public void run() {
		ClientContext.getInstance().getLogger().info("Building gui: " + this.builderInstance + "." + this.builderMethodName);
		
		if (!display.isDisposed() && this.builderInstance != null && this.builderMethod != null) {
			try {
				this.builderMethod.invoke(this.builderInstance, (Object[])null);
			}
			catch (Exception ex) {
				ClientContext.getInstance().getLogger().error("Error invoking GUI builder method!", ex);
			}
		}
	}
}
